﻿using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using SearchDialog.Properties;

namespace SearchDialog
{
    public partial class SearchDialog : Form
    {
        // Declare Public Variables
        public DataGridViewRow SelectedRow;

        // Declare Global Variables
        private readonly DataTable dataTable;

        /// <summary>
        /// Display a Search Dialog Box to the user in order to make the selection easier.
        /// </summary>
        /// <param name="dataTable">DataTable containing the information to display to the user</param>
        /// <param name="title">Title of the search Dialog Box </param>
        public SearchDialog(DataTable dataTable, string title)
        {
            // Initialize components
            InitializeComponent();
            this.dataTable = dataTable;

            // Customizing controls
            label.Text = title;

            // Settings up the data.
            bindingSource.DataSource = this.dataTable;
            dataGridView.AutoGenerateColumns = true;
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((keyData & Keys.KeyCode) == Keys.Escape)
            {
                DialogResult = DialogResult.Cancel;
                Close();
            }
            if ((keyData & Keys.KeyCode) == Keys.Enter)
            {
                DialogResult = DialogResult.OK;
                SelectedRow = dataGridView.SelectedRows[0];
                Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void textBox_TextChanged(object sender, System.EventArgs e)
        {
            //if (string.IsNullOrWhiteSpace(textBox.Text))
            //{
            //    bindingSource.DataSource = this.dataTable;
            //}
            //else
            //{
            //    var source = dataTable.AsEnumerable();

            //    var results = from matchingItem in source
            //                  where Regex.IsMatch(matchingItem.Field<string>("Doctor"), textBox.Text.Trim())
            //                  select matchingItem;

            //    //If you need them as a list when you are done (to bind to or something)
            //    bindingSource.DataSource = results;
            //    dataGridView.AutoGenerateColumns = true;
            //}
            

            //Código LIKE '%d%' OR Doctor LIKE '%d%' OR Hospital LIKE '%d%' OR Correo LIKE '%d%'

            //dataGridView.

            if (string.IsNullOrWhiteSpace(textBox.Text))
                bindingSource.RemoveFilter();
            else
            {
                //var filter = 
                //label.Text = filter;
                bindingSource.Filter = string.Join(Settings.Default.FilterConcatenation, (from DataColumn dataColumn in dataTable.Columns select string.Format(Settings.Default.SearchDialogFilter, dataColumn.ColumnName, EscapeLikeValue(textBox.Text.Trim()))).ToList());//string.Format("Doctor LIKE '%{0}%'", .Replace(' ', '%'));
            }
        }

        private void dataGridView_Click(object sender, System.EventArgs e)
        {
            DialogResult = DialogResult.OK;
            SelectedRow = dataGridView.SelectedRows[0];
            Hide();
        }

        private string EscapeLikeValue(string value)
        {
            StringBuilder sb = new StringBuilder(value.Length);
            foreach (var c in value)
            {
                switch (c)
                {
                    case ']':
                    case '[':
                    case '%':
                    case '*':
                        sb.Append("[").Append(c).Append("]");
                        break;
                    case '\'':
                        sb.Append("''");
                        break;
                    default:
                        sb.Append(c);
                        break;
                }
            }
            return sb.ToString();
        }
    }
}
